<?php
namespace app\shop\controller\api;

use app\shop\library\ApiAuthController;
use app\shop\library\Member as M;

use app\shop\library\coupon\CouponStock;
use app\shop\library\coupon\Coupon;
use app\shop\library\coupon\MemberCoupon;
use app\shop\library\order\MemberOrder;

use app\shop\library\Snowflake;
use cubox\provider\exception\LogException;

class Member extends ApiAuthController{

    public function onInfo(){
        
        $member = $this->model('Member');
        $data = M::formatFront($member->getId(M::$id));
        $data['coupon'][] = MemberCoupon::list();
        return $data;
    }

    public function onIndex(){
        $re['order'] =  MemberOrder::count();
        $re['coupon'] = MemberCoupon::count(); 
        return $re;    
    }

    public function onModifyPassword(){
        
    }

    public function onGoodsBrowsing(){
        $goodsId = $this->post('goods_id',$this->check()->type('int')->required()); 
        
        $MemberBrowsingHistory = $this->model('MemberBrowsingHistory');

        $data = $MemberBrowsingHistory ->where('member_id',M::$id)->where('goods_id',$goodsId)->getRow();

        if ($data){
            $update['num'] = $data['num'] + 1;
            $update['update_time'] = time(); 
            $MemberBrowsingHistory->updateId($data['history_id'],$update);
        }else{
            $data = [
                'member_id' => M::$id,
                'goods_id' => $goodsId,
                'num' => 1,
                'update_time' => time(),
            ];

            $MemberBrowsingHistory->insert($data);
        }


        M::updateTag();
        
        return true;
    }

    public function onCouponExist(){
        $ids = $this->post('ids',$this->check()->type('array')->required());

        return MemberCoupon::exist($ids);
    }

    public function onCouponList(){
        $page = $this->post('page',$this->check()->type('int')->required());
        $limit = $this->post('limit',$this->check()->type('int')->default(10));
        $type = $this->post('type',$this->check()->type('string')->default('all'));

        $memberCoupun =  $this->model('MemberCoupon');
        
        $memberCoupun->where('member_id',M::$id);

        switch ($type){
            case 'all':
                break;
            case 'new':
                $memberCoupun->where('status','new')->where('coupon_endtime','>',date('Y-m-d H:i:s'));
                break;
            case 'used':
                $memberCoupun->where('status','used');
                break;
            case 'expired':
                $memberCoupun->where('coupon_endtime','<',date('Y-m-d H:i:s'))->where('status','new');
                break;
            default:
        }


        $list =  $memberCoupun->order('created_at','desc')->page($page,$limit)->getData();


        foreach($list as $k=>$v){
            $list[$k] = array_merge($v,Coupon::get($v['coupon_id']));
        }

        return ['list'=>$list,'total'=>$memberCoupun->total,'count'=>$memberCoupun->pageCount];
    }


    public function onCouponReceive(){
        $coupon_id = $this->post('coupon_id',$this->check()->type('int')->required());
        try{
            Coupon::receive($coupon_id,M::$id);
            return true;
        }catch(\Exception $e){
            throw new \Exception($e->getMessage());
        }
    }
}